﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GameSite.Models
{
    public enum ConfigKey : int
    {
        CorpAddress,
        CorpEmail,
        CorpName,
        CorpQQ,
        CorpShortName,
        CorpTel,

        GameCommonNotice,
        GameVirualCoinCN,

        HallClientAndroidUrl,
        HallClientIOSUrl,
        HallClientWinPCUrl,
        HallClientWPhoneUrl,

        PlatFormName,
        PlatFormNameCN,

        SiteLogoUrl,
        SiteName,
        SiteNameCN,
        SiteTemplateName,
        SiteUrl,

        SNGameLicense,
        SNSiteLicense,
        SNVirtualPayLicense,
    }

    public enum NewsCategory : int
    {
        AllNotice,
        GameNotice,
        ActivityNotice
    }

    public static class GlobalViewModel
    {
        private static ConfigViewModel _ConfigModel = null;

        private static ConfigViewModel GetConfig(GameA4Context context)
        {
            if (_ConfigModel == null)
            {
                using (var dbContext = new GameA4Context())
                {
                    _ConfigModel = new ConfigViewModel(dbContext.SiteConfigs.ToDictionary(t => t.ConfigKey));
                }
            }

            return _ConfigModel;
        }

        private static IEnumerable<GameBan> GetGameBanInfo(GameA4Context context)
        {
            return context.GameBans.ToList();
        }

        public static ConfigViewModel ConfigModel
        {
            get
            {
                if (_ConfigModel == null)
                {
                    using (var dbContext = new GameA4Context())
                    {
                        _ConfigModel = new ConfigViewModel(dbContext.SiteConfigs.ToDictionary(t => t.ConfigKey));
                    }
                }

                return _ConfigModel;
            }
        }

        public static IEnumerable<GameBan> GameBanInfo
        {
            get
            {
                using (var dbContext = new GameA4Context())
                {
                    return GetGameBanInfo(dbContext);
                }
            }
        }

        public static void CacheHome(dynamic viewbag)
        {
            using (var dbContext = new GameA4Context())
            {
                viewbag.SiteConfig = GetConfig(dbContext);
            }
        }

        public static void CacheService(dynamic viewbag)
        {
            using (var dbContext = new GameA4Context())
            {
                viewbag.SiteConfig = GetConfig(dbContext);
            }
        }

        public static void CacheAccount(dynamic viewbag)
        {
            using (var dbContext = new GameA4Context())
            {
                viewbag.SiteConfig = GetConfig(dbContext);
            }
        }

        public static void CacheStore(dynamic viewbag)
        {
            using (var dbContext = new GameA4Context())
            {
                viewbag.SiteConfig = GetConfig(dbContext);
            }
        }

        public static void CacheDownload(dynamic viewbag)
        {
            using (var dbContext = new GameA4Context())
            {
                viewbag.SiteConfig = GetConfig(dbContext);
            }
        }

        public static void CacheNews(dynamic viewbag)
        {
            using (var dbContext = new GameA4Context())
            {
                viewbag.SiteConfig = GetConfig(dbContext);
            }
        }

        public static IEnumerable<GameNew> GetDispList(this IEnumerable<GameNew> gameNews)
        {
            return gameNews.OrderByDescending(n => n.IsTop).ThenByDescending(n => n.CreateTime);
        }

        public static IEnumerable<GameNew> GetSlideList(this IEnumerable<GameNew> gameNews)
        {
            return gameNews.Where(t => t.IsSlide == 1);
        }

        public static IEnumerable<Activity> GetDispList(this IEnumerable<Activity> activities)
        {
            return activities.Where(a => a.IsShow == 1).OrderByDescending(a => a.CreateTime);
        }
    }
}